package com.century.sjt.activity.Pay;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.webkit.HttpAuthHandler;
import android.webkit.SslErrorHandler;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.century.sjt.R;
import com.century.sjt.activity.BaseActivity;
import com.century.sjt.widget.TitleBar.SjtTitleBar;

/**
 * Created by asus on 2016/10/1.
 */

public class PayPalWindowActivity extends BaseActivity {
    private String TAG = "PayPalWindowActivity";
    private SjtTitleBar titleBar;
    private WebView mWVcontent;
    private String detailsUrl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sjt_paypal_window_activity);
        Intent intent = getIntent();
        detailsUrl = intent.getStringExtra("payPalUrl");
        initView();
        initEvent();
        initData();


    }

    @Override
    protected void initView() {
        titleBar = (SjtTitleBar) findViewById(R.id.title_bar);
        titleBar.setTitle(getResources().getString(R.string.text_phone_pay));
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mWVcontent = (WebView) findViewById(R.id.detail_webview );
    }

    @Override
    protected void initEvent() {
        WebSettings settings = mWVcontent.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWVcontent.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return false;
            }
        });
        mWVcontent.loadUrl(detailsUrl);
//        mWVcontent.loadDataWithBaseURL("", detailsUrl,"text/html","utf-8","");
    }

    @Override
    protected void initData() {
        Log.i(TAG, "-------------- loadUrl - "+detailsUrl+"  ---------------");
        mWVcontent.setWebViewClient(new WebViewClient(){
            @Override
            public void onLoadResource(WebView view, String url) {
                super.onLoadResource(view, url);
                Log.i(TAG, "onLoadResource: "+url);
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                Log.i(TAG, "onPageFinished: "+url);
            }
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                Log.i(TAG, "onPageStarted: "+url);
            }
            @Override
            public void onReceivedHttpAuthRequest(WebView view,
                                                  HttpAuthHandler handler, String host, String realm) {
                super.onReceivedHttpAuthRequest(view, handler, host, realm);
                Log.i(TAG, "onReceivedHttpAuthRequest: ");
            }
            @Override
            public void onReceivedSslError(WebView view,
                                           SslErrorHandler handler, SslError error) {
                super.onReceivedSslError(view, handler, error);
                Log.i(TAG, "onReceivedSslError: ");
            }
            //                        @Override
//                        public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                                Log.i(TAG, "shouldOverrideUrlLoading: ");
//                                return super.shouldOverrideUrlLoading(view, url);
//                        }
            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                Log.i(TAG, "onReceivedError: ");
                Log.i(TAG, "errorCode: "+errorCode);
                Log.i(TAG, "description: "+description);
                Log.i(TAG, "failingUrl: "+failingUrl);
            }
            @Override
            @Deprecated
            public void onTooManyRedirects(WebView view, Message cancelMsg,
                                           Message continueMsg) {
                super.onTooManyRedirects(view, cancelMsg, continueMsg);
                Log.i(TAG, "onTooManyRedirects");
            }



        });
        mWVcontent.setWebChromeClient(new WebChromeClient(){
            @Override
            public void getVisitedHistory(ValueCallback<String[]> callback) {
                super.getVisitedHistory(callback);
                Log.i(TAG, "getVisitedHistory");
            }
            @Override
            public void onCloseWindow(WebView window) {
                super.onCloseWindow(window);
                Log.i(TAG, "onCloseWindow");
            }
            @Override
            public boolean onCreateWindow(WebView view, boolean isDialog,
                                          boolean isUserGesture, Message resultMsg) {
                Log.i(TAG, "onCreateWindow");
                return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg);
            }
            @Override
            @Deprecated
            public void onExceededDatabaseQuota(String url,
                                                String databaseIdentifier, long quota,
                                                long estimatedDatabaseSize, long totalQuota,
                                                WebStorage.QuotaUpdater quotaUpdater) {
                super.onExceededDatabaseQuota(url, databaseIdentifier, quota,
                        estimatedDatabaseSize, totalQuota, quotaUpdater);
                Log.i(TAG, "onExceededDatabaseQuota"+url);
            }
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                Log.i(TAG, "onProgressChanged newProgress "+newProgress);
            }
            @Override
            public void onReceivedIcon(WebView view, Bitmap icon) {
                super.onReceivedIcon(view, icon);
                Log.i(TAG, "gonReceivedIcon");
            }
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                Log.i(TAG, "onReceivedTitle");
            }
            @Override
            public void onRequestFocus(WebView view) {
                super.onRequestFocus(view);
                Log.i(TAG, "onRequestFocus");
            }
        });
    }

}
